package com.elong.android.specialhouse.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;

/**
 * @ClassName: MyNotificationManager
 * @Description: Notificatoin 管理器
 * @author kaiqi.li
 * @date 2015年1月5日 下午4:05:50
 */
public class MyNotificationManager {

    private NotificationInterface mNotificationInterface ;
    private static MyNotificationManager instanse ;
    private NotificationManager notificationManager = null ;
    private static final int START_ID = 1000;
    private static final int RANGE = 50;
    private int mCurrentId = START_ID;

    private MyNotificationManager(){
    }

    public static MyNotificationManager getInstanse(){
        if (instanse == null ) {
            synchronized (MyNotificationManager.class) {
                if (instanse == null) {
                    instanse = new MyNotificationManager() ;
                }
            }
        }
        return instanse ;
    }

    public NotificationInterface getNotificationInterface(){
        return mNotificationInterface ;
    }
    public void  setNotificationInterface(NotificationInterface notificationInterface){
        mNotificationInterface = notificationInterface;
    }

    /**@description 对外发送通知的接口
     * @param context
     * @param title  标题
     * @param content  内容
     * @param iconId  图标 id
     */
    public void sendNotification(Context context ,String title , String content ,int iconId){
        buildAndDisplayNotification(context, title, content, iconId);
    }
    /**@description 创建通知
     * @param context
     * @param title  标题
     * @param content  内容
     * @param iconId  图标 id
     */
    private void buildAndDisplayNotification(Context context ,String title , String content ,int iconId){
        if (mNotificationInterface != null) {
            Notification notification = mNotificationInterface.generateNotifacationBuilder(title, content, iconId).build();
            if (notification != null) {
                if (notificationManager == null) {
                    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                }
//                notificationManager.notify(111,notification);
                notificationManager.notify(title.hashCode(), notification);
            }
        }
    }

    /**
     * @return Notification 的id
     */
    private int generateNotificationId() {
        mCurrentId++;
        if (mCurrentId >= START_ID + RANGE) {
            mCurrentId = START_ID;
        }
        return mCurrentId;
    }

    /**
     * 清除对应标题的通知栏显示
     * @param title
     */
    public void cancelNotification(String title) {
        if (notificationManager != null) {
            notificationManager.cancel(title.hashCode());
        }
    }
}
